package cn.tianyigps.tianyigpsguazi.handler;

import cn.tianyigps.tianyigpsguazi.Utils.DirectionUtils;
import cn.tianyigps.tianyigpsguazi.dao.EquipmentDao;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.manger.TaskManger;
import cn.tianyigps.tianyigpspublic.bean.EquipmentModel;
import cn.tianyigps.tianyigpspublic.util.FormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * cn.tianyigps.tianyigpsguazi.handler
 *
 * @author zgl
 * @create 2019-03-22 15:28
 */
@Component
public class TaskHandler {
    @Autowired
    EquipmentDao equipmentDao;
    @Autowired
    private TaskManger taskManger;
    private  static final String WIRED = "wired";
    private  static final String WIRELESS = "wireless";
    private  static final String GPS = "00";
    private  static final String BASE_STATION = "01";
    private  static final String WIFI = "02";
    private  static final DateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private  static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##");

    public  void consume(Task task){
        List <Map<String,Object>> maps =  new ArrayList<>();
        Map<String, Object> params = task.getParams();
        String equipmentId = (String) params.get("equipmentId");
        Date start = FormatUtil.format2Date(params.get("start"));
        Date end = FormatUtil.format2Date(params.get("end"));
        List<EquipmentModel> equipmentCheckModels = equipmentDao.listEquipmentCheckModelById(equipmentId, start, end, "equipmentCheckModel");
        // 过滤没有经纬度的数据
        List<EquipmentModel> equipmentCheckModelListResult = equipmentCheckModels.stream().filter(equipmentCheckModel ->
                (Optional.ofNullable(equipmentCheckModel.getGpsLongitude()).isPresent() && Optional.ofNullable(equipmentCheckModel.getGpsLatitude()).isPresent()))
                .collect(Collectors.toList());
        List<EquipmentModel> equipmentCheckModelList = equipmentCheckModelListResult.stream().filter(
                equipmentCheckModel -> (equipmentCheckModel.getGpsLatitude() <= 90.00)).collect(Collectors.toList());

        for (EquipmentModel equipmentCheckModel: equipmentCheckModelList){
            Map<String,Object> map = new HashMap<>();
            //必填字段
             String unique = equipmentCheckModel.getEquipmentId();
                map.put("unique",unique);
             String time = DF.format(equipmentCheckModel.getLocateTime());
                map.put("time",time);
             Boolean isHeartBeat = false;
                map.put("isHeartBeat",isHeartBeat);
             Double longitude = equipmentCheckModel.getGpsLongitude();
                map.put("longitude",longitude);
             Double latitude = equipmentCheckModel.getGpsLatitude();
                map.put("latitude",latitude);
             Double speed = equipmentCheckModel.getSpeed();
                map.put("speed",speed);
             //非必填字段
            String deviceTypeStr = equipmentCheckModel.getPowerType();
            Integer deviceType = null;
             if (!StringUtils.isEmpty(deviceTypeStr)){
                if (WIRED.equals(deviceTypeStr)){
                     deviceType = 0;

                }
                if (WIRELESS.equals(deviceTypeStr)){
                     deviceType = 1;
                }
             }
             map.put("deviceType",deviceType);

             String locationTypeStr = equipmentCheckModel.getLocationType();
             Integer locationType = null;
             if (!StringUtils.isEmpty(locationTypeStr)){
                if (GPS.equals(locationTypeStr)){
                    locationType = 0;
                }
                if (BASE_STATION.equals(locationTypeStr)){
                    locationType = 1;
                }
                if (WIFI.equals(locationTypeStr)){
                    locationType = 2;
                }
             }
             map.put("locationType",locationType);

//             String batteryStr = equipmentCheckModel.getBatteryLevel();
//             Double battery = null;
//             if (!StringUtils.isEmpty(batteryStr)){
//                batteryStr = batteryStr.substring(0,batteryStr.length()-1);
//                battery = Double.parseDouble(DECIMAL_FORMAT.format(Double.parseDouble(batteryStr)));
//             }
//             map.put("battery",battery);
             map.put("altitude",null);
             //当前无海拔数据，直接放空值

            Integer direction = null;
            String directionStr = equipmentCheckModel.getHeading();
            if (!StringUtils.isEmpty(directionStr)){
                direction = DirectionUtils.transfer(directionStr);
            }
            map.put("direction",direction);
            map.put("ext",null);
            //扩展字段为空
            maps.add(map);
        }
        Task nextTask = task.getNextTask();
        nextTask.setId(task.getId());
        Map<String, Object> nextTaskParams = nextTask.getParams();
        nextTaskParams.put("maps",maps);
        //返回List类型的map
        nextTask.setParams(nextTaskParams);
        taskManger.distributeTask(nextTask);
    }

}
